package com.haibei.view.web;

import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.WebSettings;

/**
 * webview的封装类，初始化webview,JS调取本地方法等都在此类中进行
 * 
 */
public abstract class BaseWebPageClient {

	public static final String TAG = BaseWebPageClient.class.getSimpleName();

	protected WuyouWebview mWebView;
	public WebSettings mWebSettings;
	public boolean mIsShowloading;

	private final Context mContext;
	private final Handler mHandler = new Handler();
	private final WebActionCallBack mWebCallBack;

	public Context getContext() {
		return mContext;
	}

	public WuyouWebview getWebView() {
		return mWebView;
	}

	public BaseWebPageClient(Context context, WuyouWebview webView) {
		this(context, webView, null);
	}

	public BaseWebPageClient(Context context, WuyouWebview webView, WebActionCallBack webCallBack) {
		mContext = context;
		this.mWebCallBack = webCallBack;
		this.mWebView = webView;

		mWebSettings = webView.getSettings();

		mWebSettings.setDefaultTextEncodingName("utf-8");
		mWebView.addJavascriptInterface(new JsObject(), "stub");

		mWebSettings.setSavePassword(false);
		mWebSettings.setSaveFormData(false);
		mWebSettings.setSupportZoom(false);
		mWebSettings.setJavaScriptEnabled(true);
		/*** 打开本地缓存提供JS调用 **/
		mWebView.getSettings().setDomStorageEnabled(true);
		mWebView.getSettings().setAppCacheMaxSize(1024 * 1024 * 8);
		String appCachePath = context.getApplicationContext().getCacheDir().getAbsolutePath();
		mWebView.getSettings().setAppCachePath(appCachePath);
		mWebView.getSettings().setAllowFileAccess(true);
		mWebView.getSettings().setAppCacheEnabled(true);
	}

	public void loadUrl(String url, boolean isShowLoading) {
		mIsShowloading = isShowLoading;
		try {
			if (!TextUtils.isEmpty(url)) {
				Log.d(TAG, "---------load url end---------:" + url);
				mWebView.pageUp(true);// 为了解决筛选重新刷数据时将页面置为从头开始
				mWebView.loadUrl(url, CookiesManager.createHeader(mContext));
			}
		} catch (Exception e) {
			Log.d(TAG, "HybridWebPage loadUrl exception mag:" + e.getMessage());
		}
	}

	/**
	 * 直接加载，不进行formatUrl
	 * 
	 * @param url
	 */
	public void directLoadUrl(String url) {
		mWebView.loadUrl(url);
	}

	public boolean webCanGoBack() {
		return mWebView.canGoBack();
	}

	public void webGoBack() {
		mWebView.goBack();
	}

	public void clearCahe(boolean includeDiskFiles) {
		mWebView.clearCache(includeDiskFiles);
	}

	public void loadData(String data, String mimeType, String encoding) {
		mWebView.loadData(data, mimeType, encoding);
	}

	public String getTitle() {
		return mWebView.getTitle();
	}

	/**
	 * 供JS调用的本地类
	 * 
	 * @author Administrator
	 */
	class JsObject {
		public void jsCallMethod(String s) {
			Log.d(TAG, "json:" + s);
			doClient(s); // 此处实现由实现此框架的客户端去完成
		}

	}

	/**
	 * 客户端解析JS端传递的json对象并做相应的操作，由业务层子类具体实现
	 * 
	 * @param s
	 *            JS传递的json字符串
	 */
	public void doClient(String s) {
		Log.d(TAG, "json=============" + s);
		// WebActionManager.getInstance().doWebAction(s, getContext(), mHandler,
		// mWebCallBack);
	}

	/**
	 * 显示网页加载的loading
	 */
	public abstract void showRequestLoading();

	/**
	 * 隐藏网页加载的loading
	 */
	public abstract void hideRequestLoading();
}
